﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using RockCompany.Master.Model;
using RockCompany.Master.Controller;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Common.Forms;


namespace RockCompany.Master.View
{
    public partial class SI02B : ParentFrm
    {
        #region Properties
        private Translator trans;
        private string sUserName; // login user
        private UserAuthenticate uAuth;
        private string sSelectMatOrder;
        private SI02BFormModel frmModel;
        private SI02A newFrm;
        #endregion

        #region Constructors
        public SI02B()
        {
            InitializeComponent();
            sUserName = "";
            trans = new Translator();
            initDefaultData();
        }

        public SI02B(Translator translator, string sUName, Form caller)
            : base(sUName, caller)
        {
            sUserName = sUName;
            trans = translator;
            Owner = caller;
            InitializeComponent();
            initDefaultData();
        }
        #endregion

        #region Event Methods
        private void button_SearchWorkOrder_Click(object sender, EventArgs e)
        {
            loadMatOrderList();
        }

        private void dataGridView_WOList_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                sSelectMatOrder = dataGridView_MatOrderList.CurrentRow.Cells[PHIEU_LANH_NVL_DTO.N_MS_PHIEU_LANH_NVL].EditedFormattedValue.ToString();
                loadWorkOrderDetail(sSelectMatOrder);
            }
        }

        private void button_NewMatOrder_Click(object sender, EventArgs e)
        {
            newFrm = new SI02A(trans, sUserName, this);
            newFrm.initDefaultData();
            newFrm.Show();
        }

        private void button_ModMatOrder_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_MODIFY)))
            {
                if (dataGridView_MatOrderList.CurrentRow != null)
                {
                    newFrm = new SI02A(trans, sUserName, this);
                    newFrm.initExistData(dataGridView_MatOrderList.CurrentRow, frmModel.MatsOrderDetail);
                    newFrm.initDefaultData();
                    newFrm.Show();
                }
            }
        }

        private void button_DelMatOrder_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_DELETE)))
            {
                bool b1 = frmModel.removeMatOrderDetail(sSelectMatOrder), b2 = frmModel.removeMatOrder(sSelectMatOrder);
                if (sSelectMatOrder != "" && (b1 || b2))
                {
                    InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    loadMatOrderList();
                }
                else
                    InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            }
        }

        private void button_PrintRP_Click(object sender, EventArgs e)
        {
            if (dataGridView_MatOrderList.CurrentRow != null)
            {
                int index = dataGridView_MatOrderList.CurrentRow.Index;
                PHIEU_LANH_NVL_DTO curOb = new PHIEU_LANH_NVL_DTO();
                curOb.MS_PHIEU_LANH_NVL = frmModel.MatsOrderList.Rows[index][PHIEU_LANH_NVL_DTO.N_MS_PHIEU_LANH_NVL].ToString();
                curOb.NGUOI_LANH_NVL = frmModel.MatsOrderList.Rows[index][PHIEU_LANH_NVL_DTO.N_NGUOI_LANH_NVL].ToString();
                curOb.NGAY_LANH_NVL = DateTime.Parse(frmModel.MatsOrderList.Rows[index][PHIEU_LANH_NVL_DTO.N_NGAY_LANH_NVL].ToString());
                if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_PRINT)))
                {

                    if (frmModel.export2Excel(InforDialog.showSaveFileDlg(this), curOb))
                    {
                        InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.SaveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    }
                    else
                        InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.SaveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Other functions
        private void initDefaultData()
        {
            frmModel = new SI02BFormModel();
            frmModel.Owner = this;
            uAuth = new UserAuthenticate();
            uAuth.setRights(this, sUserName);
            trans.controlTranslate(this);
            frmModel.translator = this.trans;

            dateTimePicker_ToDate.Value = DateTime.Today;
            dateTimePicker_FromDate.Value = DateTime.Today.AddDays(-1);    

            PHIEU_LANH_NVL_DTO temp = new PHIEU_LANH_NVL_DTO();
            List<System.Reflection.PropertyInfo> properties = new List<System.Reflection.PropertyInfo>();
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_DTO.N_MS_PHIEU_LANH_NVL));
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_DTO.N_PHE_DUYET));
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_DTO.N_NGAY_LANH_NVL));
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_DTO.N_NGUOI_LANH_NVL));
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_DTO.N_THU_KHO));
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_DTO.N_CREATION_TIME));
            properties.Add(temp.GetType().GetProperty(PHIEU_LANH_NVL_DTO.N_CREATION_USER));
            frmModel.buildDataGrid(dataGridView_MatOrderList, properties, null);

            properties.Clear();

            PHIEU_LANH_NVL_CT_DTO temp1 = new PHIEU_LANH_NVL_CT_DTO();
            properties.Add(temp1.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_MA_NVL_VTU));
            properties.Add(temp1.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_SO_LO));
            properties.Add(temp1.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_DON_VI));
            properties.Add(temp1.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_SL_DU_LANH));
            properties.Add(temp1.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_SL_THUC_LANH));
            properties.Add(temp1.GetType().GetProperty(PHIEU_LANH_NVL_CT_DTO.N_GHI_CHU));
            frmModel.buildDataGrid(dataGridView_MatOrderDetails, properties, null);
        }

        private void loadMatOrderList()
        {
            string sFromDate = dateTimePicker_FromDate.Value.ToString("yyyyMMdd");
            string sToDate = dateTimePicker_ToDate.Value.ToString("yyyyMMdd");
            dataGridView_MatOrderList.DataSource = frmModel.searchWorkOrder(sFromDate, sToDate);
        }

        private void loadWorkOrderDetail(string s)
        {
            dataGridView_MatOrderDetails.DataSource = frmModel.searchWorkOrderDetails(s);
        }
        #endregion        
    }
}
